/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.DTO.OutputExcelDTO;
import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.domain.EnterpriseType;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentExcel;
import cn.edu.nju.software.greencreditsystem.domain.FinancialExcel;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseService;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseTypeService;
import cn.edu.nju.software.greencreditsystem.service.EnvironmentExcelService;
import cn.edu.nju.software.greencreditsystem.service.FinancialExcelService;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;

/**
 * 作者：韩天
 * 创建时间：2011-8-15
 * 类描述：银行数据提交模块的action封装类
 */
public class BankUploadAction extends ActionSupport implements ServletContextAware {
    private int enterpriseId;//企业的id
    private boolean passed;// 是否通过报表
    private boolean isFinancial;//审核的是否为财务报表
    private int excelId;// 报表的id
    private List<FinancialExcel> fExcelList;//  企业财务报表list
    private List<EnvironmentExcel> eExcelList;//   企业环境报表
    private List<EnterpriseType> typeList;//  企业类型list
    private List<Enterprise> enterpriseList;// 所有企业
    private List<OutputExcelDTO> outputExcelList;//方便前台显示的excel报表list
    
    private ServletContext context;
    private String fileName;//要打开文件名

    
    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute() {
        return SUCCESS;
    }
    
    /**
     * 方法描述：查看财务报表
     * param：
     * return：SUCCESS
     */
    public String lookup_Excel(){
        EnterpriseTypeService ets = ServiceFactory.getEnterpriseTypeServiceInstance();
        this.typeList = ets.getTypeList();
        EnterpriseService es = ServiceFactory.getEnterpriseServiceInstance();
        this.enterpriseList = es.getAllEnterprise();

        return SUCCESS;
    }
    
    /**
     * 方法描述：查看财务报表
     * param：
     * return：SUCCESS
     */
    public String lookup_UncheckedFinancail(){
        FinancialExcelService fes = ServiceFactory.getFinancialExcelServiceInstance();
        this.fExcelList = fes.getUnckeckedExcel(enterpriseId);
        
        this.outputExcelList = new ArrayList<OutputExcelDTO>();
        for(int i=0; i<fExcelList.size(); i++){
            OutputExcelDTO temp = new OutputExcelDTO();
            temp.setId(fExcelList.get(i).getId());
            temp.setIsFinancial(true);
            temp.setFileName(fExcelList.get(i).getSubmitDate().toString()+"财务报表.xls");
            temp.setDate(fExcelList.get(i).getSubmitDate());
            temp.setPassed(fExcelList.get(i).getPassed());
            outputExcelList.add(temp);
        }
        return SUCCESS;
    }
    
    /**
     * 方法描述：查看财务报表
     * param：
     * return：SUCCESS
     */
    public String lookup_CheckedFinancail(){
        FinancialExcelService fes = ServiceFactory.getFinancialExcelServiceInstance();
        this.fExcelList = fes.getCkeckedExcel(enterpriseId);
        
        this.outputExcelList = new ArrayList<OutputExcelDTO>();
        for(int i=0; i<fExcelList.size(); i++){
            OutputExcelDTO temp = new OutputExcelDTO();
            temp.setId(fExcelList.get(i).getId());
            temp.setIsFinancial(true);
            temp.setFileName(fExcelList.get(i).getSubmitDate().toString()+"财务报表.xls");
            temp.setDate(fExcelList.get(i).getSubmitDate());
            temp.setPassed(fExcelList.get(i).getPassed());
            outputExcelList.add(temp);
        }
        return SUCCESS;
    }
    
    /**
     * 方法描述：查看环境报表
     * param：
     * return：SUCCESS
     */
    public String lookup_UncheckedEnvironment(){
        EnvironmentExcelService ees = ServiceFactory.getEnvironmentExcelServiceInstance();
        this.eExcelList = ees.getUnckeckedExcel(enterpriseId);      
        
        this.outputExcelList = new ArrayList<OutputExcelDTO>();
        for(int i=0; i<eExcelList.size(); i++){
            OutputExcelDTO temp = new OutputExcelDTO();
            temp.setId(eExcelList.get(i).getId());
            temp.setIsFinancial(false);
            temp.setFileName(eExcelList.get(i).getSubmitDate().toString()+"环境报表.xls");
            temp.setDate(eExcelList.get(i).getSubmitDate());
            temp.setPassed(eExcelList.get(i).getPassed());
            outputExcelList.add(temp);
        }
        return SUCCESS;
    }
    
    /**
     * 方法描述：查看环境报表
     * param：
     * return：SUCCESS
     */
    public String lookup_CheckedEnvironment(){
        EnvironmentExcelService ees = ServiceFactory.getEnvironmentExcelServiceInstance();
        this.eExcelList = ees.getCkeckedExcel(enterpriseId);       
        
        this.outputExcelList = new ArrayList<OutputExcelDTO>();
        for(int i=0; i<eExcelList.size(); i++){
            OutputExcelDTO temp = new OutputExcelDTO();
            temp.setId(eExcelList.get(i).getId());
            temp.setIsFinancial(false);
            temp.setFileName(eExcelList.get(i).getSubmitDate().toString()+"环境报表.xls");
            temp.setDate(eExcelList.get(i).getSubmitDate());
            temp.setPassed(eExcelList.get(i).getPassed());
            outputExcelList.add(temp);
        }
        return SUCCESS;
    }
    
    /**
     * 方法描述：审核报表
     * param：
     * return：SUCCESS
     */
    public String check_Excel(){
        if(this.isFinancial){
            FinancialExcelService fes = ServiceFactory.getFinancialExcelServiceInstance();
            fes.checkExcel(this.excelId, passed, enterpriseId);
            return "financial";
        }else{
            EnvironmentExcelService ees = ServiceFactory.getEnvironmentExcelServiceInstance();
            ees.checkExcel(this.excelId, passed, enterpriseId);
            return "environment";
        }
        
    }
    
    
    /**
     * 方法描述：银行打开报表审核
     * param：
     * return：SUCCESS
     */
    public String download_Excel(){
        if(isFinancial){
            FinancialExcelService fes = ServiceFactory.getFinancialExcelServiceInstance();
            FinancialExcel fExcel = fes.get_Excel(excelId);
            this.fileName = fExcel.getSubmitDate().toString()+"财务报表.xls";
            File temp = new File(context.getRealPath("/"), fileName);

            try {
                FileOutputStream foStream = new FileOutputStream(temp);
                foStream.write(fExcel.getData());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }else{
            EnvironmentExcelService ees = ServiceFactory.getEnvironmentExcelServiceInstance();
            EnvironmentExcel eExcel = ees.get_Excel(excelId);
            this.fileName = eExcel.getSubmitDate().toString()+"环境报表.xls";
            File temp = new File(context.getRealPath("/"), fileName);
            
            try {
                FileOutputStream foStream = new FileOutputStream(temp);
                foStream.write(eExcel.getData());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return SUCCESS;
    }
    
    
    
    
    public InputStream getInputStream() {
        return ServletActionContext.getServletContext().getResourceAsStream("/" + fileName);
    }
    
    public List<EnvironmentExcel> geteExcelList() {
        return eExcelList;
    }

    public void seteExcelList(List<EnvironmentExcel> eExcelList) {
        this.eExcelList = eExcelList;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public void setEnterpriseId(String enterpriseId){
        this.enterpriseId = Integer.valueOf(enterpriseId);
    }
    
    public List<FinancialExcel> getfExcelList() {
        return fExcelList;
    }

    public void setfExcelList(List<FinancialExcel> fExcelList) {
        this.fExcelList = fExcelList;
    }

    public int getExcelId() {
        return excelId;
    }

    public void setExcelId(int excelId) {
        this.excelId = excelId;
    }
    
    public void setExcelId(String excelId){
        setExcelId(Integer.valueOf(excelId));
    }

    public boolean isPassed() {
        return passed;
    }

    public void setPassed(boolean passed) {
        this.passed = passed;
    }

    public List<Enterprise> getEnterpriseList() {
        return enterpriseList;
    }

    public void setEnterpriseList(List<Enterprise> enterpriseList) {
        this.enterpriseList = enterpriseList;
    }

    public List<EnterpriseType> getTypeList() {
        return typeList;
    }

    public void setTypeList(List<EnterpriseType> typeList) {
        this.typeList = typeList;
    }

    public List<OutputExcelDTO> getOutputExcelList() {
        return outputExcelList;
    }

    public void setOutputExcelList(List<OutputExcelDTO> outputExcelList) {
        this.outputExcelList = outputExcelList;
    }

    public boolean isIsFinancial() {
        return isFinancial;
    }

    public void setIsFinancial(boolean isFinancial) {
        this.isFinancial = isFinancial;
    }

    @Override
    public void setServletContext(ServletContext sc) {
        this.context = sc;
    }

    public ServletContext getContext() {
        return context;
    }

    public void setContext(ServletContext context) {
        this.context = context;
    }

    public String getFileName() {
        try {
            fileName = new String(fileName.getBytes("GBK"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}
